#include "define.h"

Status InitStack(SqStack &S){
    S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base) exit(FAIL);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return SUCCESS;
}
Status Push(SqStack &S, SElemType e){
    if(S.top-S.base>=S.stacksize){
        S.base = (SElemType *)realloc(S.base, (S.stacksize+STACKINCREAMENT)*sizeof(SElemType));
        if(!S.base) exit(FAIL);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREAMENT;
    }
    *S.top++ = e;
    return SUCCESS;
}
Status Pop(SqStack &S, SElemType &e){
    if(S.top == S.base) return FAIL;
    e = *--S.top;
    return SUCCESS;
}
Status GetTop(SqStack S, SElemType &e){
    if(S.top==S.base) return FAIL;
    e = *(S.top-1);
    return SUCCESS;
}

//app
void conversion(){
    SqStack S;
    int N = 0, e = 0;
    InitStack(S);
    cout << "Input a number:" << endl;
    cin >> N;
    while(N){
        Push(S, N%8);
        N /= 8;
    }
    while(S.top!=S.base){
        Pop(S, e);
        cout << e;
    }
    cout << endl << "Done" << endl;
}
void LineEdit(){
    stack<char> s, s_inverse;
    char ch;
    while((ch=cin.get())!='\n'){
        switch(ch){
            case '#':
                s.pop();
                break;
            case '@':
                while(!s.empty()) s.pop();
                break;
            default:
                s.push(ch);
        }
    }
    while(!s.empty()){
        s_inverse.push(s.top());
        s.pop();
    }
    while(!s_inverse.empty()){
        cout << s_inverse.top();
        s_inverse.pop();
    }
}
Status ParenCheck(string str){
    stack<char> s;
    for(auto ch : str){
        if(ch=='('||ch=='{')
            s.push(ch);
        else if(ch==')'){
            if(s.top()=='(') s.pop();
        }
        else if(ch=='}')
            if(s.top()=='{') s.pop();
    }
    if(s.empty()) return SUCCESS;
    else return FAIL;
}
